package test;

import static org.junit.Assert.*;
import hospital.Hospital;

import movement.Location;

import org.junit.Before;
import org.junit.Test;
import unit.*;

import dispatch.DispatchCenter;
import emergency.*;


public class testPoliceCar {
	
	DispatchCenter center;
	PoliceCar policecar;
	Garage garage;
	Emergency em;
	
    @Before
    public void setUp() throws Exception { 
    	center = new DispatchCenter("DC 1",new Location(45,45,"DC 1 - Location"),new Location(60,60,"GarageLocation"));
    	garage = new Garage(new Location(0,0,"Garage"), center);
    	policecar = new PoliceCar("PoliceCar one", 0, garage);
    }

	@Test (expected=IllegalArgumentException.class) 
	public void testAssignHospital(){
		Hospital h = new Hospital("GasthuisBerg", new Location(120,120,"Locatie"));
		policecar.assignHospital(h);
	}
	
	@SuppressWarnings("unused")
	@Test (expected=IllegalArgumentException.class) 
	public void testCreateAmbulanceNullGarage(){
		PoliceCar pc = new PoliceCar("pc",0,null);
	}
	
	@SuppressWarnings("unused")
	@Test (expected=IllegalArgumentException.class) 
	public void testCreateAmbulanceNullName(){
		PoliceCar pc = new PoliceCar(null,0,garage);
	}
	
	@Test
    public void testGettersAndSetters() {
    	Location secondLocation = new Location(60,60,"SecondLocation");
    	assertEquals(policecar.isAssigned(),false);
    	assertTrue(policecar.getCurrentLocation().equals(garage.getLocation()));
    	assertEquals(policecar.getHomeLocation(), garage);
    	assertEquals(policecar.getTargetLocation(), null);
    	assertEquals(policecar.getName(), "PoliceCar one");
    	assertEquals(policecar.getSpeed(),10);
    	Garage garage2 = new Garage(new Location(10,10,"Epla"), new DispatchCenter("DC", new Location(10,10,"Epla"), new Location(12,320,"Epl2a")));
    	policecar.setCurrentLocation(secondLocation);
    	policecar.setHomeLocation(garage2);
    	policecar.setTargetLocation(secondLocation);
    	policecar.setName("PoliceCar two");
    	policecar.setSpeed(10);
    	
    	assertTrue(policecar.getCurrentLocation().equals(secondLocation));
    	assertEquals(policecar.getHomeLocation(), garage2);
    	assertEquals(policecar.getTargetLocation(), secondLocation);
    	assertEquals(policecar.getName(), "PoliceCar two");
    	assertEquals(policecar.getSpeed(),10);
    }
	
	@Test (expected=IllegalAccessException.class)
	public void testRevokeInvalidPoliceCar() throws IllegalAccessException{
		Unit u1 = new PoliceCar("Policecar-1", 100, garage);
		Emergency em1 = new Robbery(new Location(100,100,"Robbery"), Severity.BENIGN, center, false, false);
		u1.setCurrentLocation(em1.getLocation());
		u1.assignEmergency(em1);
		u1.revokeUnit();
	}
	
	@Test (expected=IllegalArgumentException.class) 
    public void testSetInvalidCurrentLocation(){
    	policecar.setCurrentLocation(null);
    }
	
	@Test (expected=IllegalArgumentException.class) 
    public void testSetInvaliHomeLocation(){
    	policecar.setHomeLocation(null);
	}
	
	@Test
    public void testSetNegativeSpeed(){
    	policecar.setSpeed(-20);
    	assertEquals(10, policecar.getSpeed());
    }
	
	@Test 
	public void testToString(){
		assertEquals("PoliceCar-PoliceCar one",policecar.toString());
	}
	
	@Test
	public void testUnitOfSameTypeOnEmergency() throws IllegalAccessException{
		PoliceCar pc2 = new PoliceCar("PC2", 50, garage);
		Location loc = new Location(36,36,"Loc");
		
		em = new emergency.PublicDisturbance(loc, Severity.NORMAL, center, 7);
		
		em.assignAllUnits(garage);

		//fails if not all policecars are on the emergency
		assertFalse(policecar.unitsOfSameTypeOnEmergency());
		
		policecar.setCurrentLocation(em.getLocation());
		
		assertFalse(policecar.unitsOfSameTypeOnEmergency());
		
		pc2.setCurrentLocation(em.getLocation());

		//returns true if all policecars are on the emergency
		assertTrue(policecar.unitsOfSameTypeOnEmergency());
	}
	
	@Test (expected=IllegalArgumentException.class)
	public void testUnitOfSameTypeOnEmergencyNoEmergency(){
		policecar.unitsOfSameTypeOnEmergency(); // policecar doesn't have an emergency yet 
	}
}
